﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Semantics.Base;
namespace Semantics.Parse
{
    public class SemanticNodeToLattice
    {
        List<LangNode> LangNodeList = new List<LangNode>();
        public List<LangNode> ExtractLangNode(List<SemanticNode> semanticnodes)
        {
            for (int i = 0; i < semanticnodes.Count; i++)
            {
                Hash(semanticnodes[i].LanguageNode);
            }

            return LangNodeList;
        }
        private void Hash(LangNode LanguageNode)
        {
            if (!LangNodeList.Contains(LanguageNode))
            {
                LangNodeList.Add(LanguageNode);
            }
            for (int i = 0; i < LanguageNode.Children.Count; i++)
            {
                Hash(LanguageNode.Children[i]);
            }
        }
        //LatticeNode ln1=new LatticeNode(0x0000000000000005L, "abcde");
        //LatticeNode ln2=new LatticeNode(0x0000000000000003L, "abc");
        //lattice.InsertConnection(ln1, ln2);
        List<LatticeNode> LatticeNodeList;
        public List<LatticeNode> ExtractLatticeNode(List<LangNode> LangNodeList)
        {
            LatticeNodeList = new List<LatticeNode>();
            int i;
            int index;
            for (i = 0; i < LangNodeList.Count; i++)
            {
                AddLaticeNode(LangNodeList[i]);
            }
            //为每个LatticeNode添加CHildren、Parent、NextSibling,PrevSibling
            for (i = 0; i < LatticeNodeList.Count; i++)
            {
                int SN = Convert.ToInt32(LatticeNodeList[i].Key);
                index = findIndexInLangNodeList(SN);
                if (index != -1)
                {
                    for (int j = 0; j < LangNodeList[index].Children.Count; j++)
                    {
                        int ChildSN = LangNodeList[index].Children[j].SN;
                        AddLatticeNodeChildren(SN, i);
                    }
                }
            }
            return LatticeNodeList;
        }
        void AddLatticeNodeChildren(int SN, int LatticeNodeListIndex)
        {
            int index = findIndexInLangNodeList(SN);
            LatticeNode child = LatticeNodeList[index];
            LatticeNodeList[LatticeNodeListIndex].Children.Add(child);

        }
        int findIndexInLangNodeList(int SN)
        {
            int index = -1;
            for (int i = 0; i < LangNodeList.Count; i++)
            {
                if (LangNodeList[i].SN == SN)
                {
                    index = i;
                    break;
                }
            }
            return index;
        }
        void AddLaticeNode(LangNode langNode)
        {
            LatticeNode NewLatticeNode = new LatticeNode(langNode.SN, langNode.Value);
            LatticeNodeList.Add(NewLatticeNode);
        }
        Lattice lattice;
        public Lattice ConstructLattice(List<LangNode> LangNodeList)
        {
            lattice = new Lattice();
            return lattice;
        }

    }
}

